import Vue from 'vue';
import Vuex from 'vuex';
Vue.use(Vuex);

const store = new Vuex.Store({
  state: {
    // 存储userId
    userId: sessionStorage.getItem('userId') ? sessionStorage.getItem('userId') : null,
    homeortestName: sessionStorage.getItem('homeortestName') ? sessionStorage.getItem('homeortestName') : '',
    homeortestId: sessionStorage.getItem('homeortestId') ? sessionStorage.getItem('homeortestId') : null,
  },

  mutations: {
    // 修改userId，并将userId存入sessionStorage
    changeLogin(state, user) {
      state.userId = user.userId;
      sessionStorage.setItem('userId', user.userId);
    },
    clear(state) {
      state.userId = null;
      state.homeortestName = '';
      state.homeortestId = null;
    },

    // 
    changeName(state, user) {
      state.homeortestName = user.name;
      sessionStorage.setItem('homeortestName', user.name);
    },

    // 
    changeId(state, user) {
      state.homeortestId = user.id;
      sessionStorage.setItem('homeortestId', user.id);
    }
  }
});

export default store;